#include <stdio.h>
#include <stdlib.h>

#define MAX 20
typedef int ElemType;

typedef struct SqList{
    ElemType data[MAX];
    int length;
}SqList;

void InitList(SqList *L){
    L->length = 0;
}

int Insert(SqList *L, int k, ElemType x){
    if(L->length == MAX){
        printf("L.length == MAX !\n");
        return 0;
    }
    if(k<0 || k>L->length+1){
        printf("k<0 || k>L.length !\n");
        return 0;
    }
    for (int i = L->length-1; i >= k-1; i--)
        L->data[i+1] = L->data[i];
    L->data[k-1] = x;
    L->length++;
    return 1;
}

void ShowArry(SqList *L){
    for (int i = 0; i < L->length; i++)
        printf("a[%d] == %d\n",i,L->data[i]);
}

void MoveLeft(SqList *L, int p){
    int i = 0; //标记当前处理的元素的下标
    int j = 0; //被交换的元素的下标
    int k = 0; //标记发生交换的次数
    ElemType tmp_in; //储存要替换进去的元素
    ElemType tmp_out; //储存被替换的元素
    tmp_in = L->data[i];
    while(k <= L->length){
        j = (i + L->length - p)%L->length; //计算发生替换的位置
        tmp_out = L->data[j]; //保存被替换的元素
        L->data[j] = tmp_in; //替换元素
        tmp_in = tmp_out; //交换被替换的元素准备下次替换
        i = j; //下一个左移的目标
        k++; //计数
    }
}

int main(){
    int i,j,k;
    SqList L;
    InitList(&L);
    for (int i = 1; i <= 10; i++)
        Insert(&L,1,10-i);
    ShowArry(&L);
    MoveLeft(&L, 3);
    printf("after moveLeft:\n");
    ShowArry(&L);
    system("pause");
}

